/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package researchgrants.parts.pi;

import java.awt.Frame;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import researchgrants.parts.LoggedData.LoggedDataOutSerializable;


/**
 * A list of Grant Requests.
 * @author ohad
 */
public class PisList implements Collection<Pi>, Cloneable, LoggedDataOutSerializable{

    @Override
    @SuppressWarnings("unchecked")
    protected Object clone() throws CloneNotSupportedException {
        PisList newList=new PisList((Collection<Pi>)pisList.clone());
        return newList;
    }

    ArrayList<Pi> pisList;

    PisList() {
        pisList=new ArrayList<Pi>();
    }

    PisList(Collection<Pi> c) {
        pisList=new ArrayList<Pi>(c);
    }
    
    @Override
    public int size() {
        return (pisList.size());
    }

    @Override
    public boolean isEmpty() {
        return (pisList.isEmpty());
    }

    @Override
    public boolean contains(Object o) {
        return (pisList.contains(o));
    }

    @Override
    public Iterator<Pi> iterator() {
        return (pisList.iterator());
    }

    @Override
    public Object[] toArray() {
        return (pisList.toArray());
    }

    @Override
    public <T> T[] toArray(T[] a) {
        T[] res=(T[]) pisList.toArray(a);
        return(res);
    }

    @Override
    public boolean add(Pi e) {
       return(pisList.add(e));
    }

    @Override
    public boolean remove(Object o) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        return(pisList.containsAll(c));
    }

    @Override
    public boolean addAll(Collection<? extends Pi> c) {
        return(pisList.addAll(c));
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void clear() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    public void show(Frame parent, String title) {
        new PisListView(parent, this, title);
    }
    
    public static PisList fromDelimitedPisList(String delimitedPiIdsList) {
        PisList newList=new PisList();
        if (!delimitedPiIdsList.equals("")) {
            String[] PiIdsList = delimitedPiIdsList.split(";");
            for (int i = 0; i < PiIdsList.length; i++) {
                String PiIdStr = PiIdsList[i];
                int piId=Integer.parseInt(PiIdStr);
                newList.add(Pi.getById(piId));
            }
        }
        return(newList);
    }
    
    public String toDelimitedPisList() {
        Pi[] arrPis=pisList.toArray(new Pi[]{});
        StringBuffer resIds=new StringBuffer();
        for (int j = 0; j < arrPis.length; j++) {
            Pi pi = arrPis[j];
            int piId=pi.getId();
            resIds.append(piId);
            if (j<arrPis.length-1) {
                resIds.append(";");
            }
        }
        return(resIds.toString());
    }

    public String toLoggedData() {
        return(toDelimitedPisList());
    }
    
    public static PisList fromLoggedData(String loggedData) {
        return(fromDelimitedPisList(loggedData));
    }
}
